/**
 * 
 */
package com.taobao.client.pages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.HeadPanel;
import com.taobao.client.RPC;
import com.taobao.client.sdk.ReceiptConfirmRequest;
import com.taobao.client.util.PopBox;

/**
 * A composite that displays the page for the buyer to confirm the receipt of the goods
 * Apply the singleton design pattern
 * 
 * @author hujingfei.pt
 *
 */
public class GoodsConfirmation extends Composite {
	private static GoodsConfirmation goodConfirmation;		
	private final static String RESULT_FLAG = "ResultCode";	
	public final static String HISTORY_ITEM = "GoodConfirmationPage";	
	private Frame f;	
	public ReceiptConfirmRequest input;	
	
	// Static method to return the static single object. The entry point for external classes
	public static GoodsConfirmation getGoodConfirmation() {
		if (goodConfirmation == null) {
			goodConfirmation = new GoodsConfirmation();
		}
		return goodConfirmation;
	}
	
	/**
	 * Set the trade id and the refund id.
	 * 
	 * @param bizOrderId The trade id.
	 */
	public void setTradeId(String bizOrderId) {
		input.setBiz_order_id(bizOrderId);
		
		RPC.getService().getUrl(input.getURLMap(), new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable arg0) {
				PopBox.error("Construct URL Error", arg0.getMessage());
			}

			@Override
			public void onSuccess(String arg0) {
//				f.setUrl(arg0);
//				f.setSize(Data.width, Data.height);
				PopBox.openWindow(arg0);
			}
		});
	}

	/**
	 * Private constructor
	 */
	private GoodsConfirmation() {
		String queryString = Window.Location.getQueryString();	
		System.out.println("GoodsConfirmation::Querystring:\t" + queryString);
		
		if (queryString != null && queryString.indexOf(RESULT_FLAG) >= 0) {
			// TODO Process the result
		} else {
			initialize();
		}
	}

	/**
	 * Initialize the GUI
	 */
	private void initialize() {
		VerticalPanel content = new VerticalPanel();
		content.add(createHead("Confirm Goods"));
		
		f = new Frame();
		content.add(f);

		initWidget(content);
		
		// Initialize the input parameters
		input = new ReceiptConfirmRequest();
		input.initialize();	
	}

	/**
	 * Create a head panel
	 * 
	 * @param title the text to be displayed in the head panel
	 * @return a panel that is displayed in the head part always
	 */
	private HeadPanel createHead(String title) {
		HeadPanel head = new HeadPanel();
		head.add(new Label(title));
		return head;
	}
}
